import Const from "../application/constants/Const";
import Pixel from "../renderExtension/types/Pixel";
import Browser from "./Browser";
import Helper from "./Helper";

/** dom对象操作类 */
export default class DomUtils {
    public static TRANSFORM = DomUtils.testProp(['transform', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']);
    /** 获取dom对象 */
    public static get(id: string) {
        return document.getElementById(id);
    }
    /** 获取dom对象样式 */
    public static getStyle(el: any, style: string) {
        let value = el.style[style] || (el.currentStyle && el.currentStyle[style]);
        if ((!value || value === 'auto') && document.defaultView) {
            let css = document.defaultView.getComputedStyle(el, null);
            value = css ? css[style] : null;
        }
        return value === 'auto' ? null : value;
    }
    /** 设置dom对象样式 */
    public static setStyle(el: any, style: {}) {
        Object.assign(el.style, style);
        return el;
    }
    /** 创建tag对象 */
    public static create(tagName: string, className?: string, container?: any) {
        let el = document.createElement(tagName);
        el.className = className || '';
        if (container) {
            container.appendChild(el);
        }
        return el;
    }
    /** 删除对象 */
    public static remove(el: any) {
        let parent = el.parentNode;
        if (parent) {
            parent.removeChild(el);
        }
    }
    /** 置空dom子集 */
    public static empty(el: any) {
        while (el.firstChild) {
            el.removeChild(el.firstChild);
        }
    }
    /** dom对象置前 */
    public static toFront(el: any) {
        let parent = el.parentNode;
        if (parent.lastChild !== el) {
            parent.appendChild(el);
        }
    }
    /** dom对象置尾 */
    public static toBack(el: any) {
        let parent = el.parentNode;
        if (parent.firstChild !== el) {
            parent.insertBefore(el, parent.firstChild);
        }
    }
    /** 判断dom对象是否存在class */
    public static hasClass(el: any, name: string) {
        if (el.classList !== undefined) {
            return el.classList.contains(name);
        }
        let className = this.getClass(el);
        return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className);
    }
    /** 添加dom对象class */
    public static addClass(el: any, name: string) {
        if (el.classList !== undefined) {
            let classes = Helper.splitWords(name);
            for (let i = 0, len = classes.length; i < len; i++) {
                el.classList.add(classes[i]);
            }
        } else if (!this.hasClass(el, name)) {
            let className = this.getClass(el);
            this.setClass(el, (className ? className + ' ' : '') + name);
        }
    }
    /** 移除dom对象class */
    public static removeClass(el: any, name: string) {
        if (el.classList !== undefined) {
            el.classList.remove(name);
        } else {
            this.setClass(el, Helper.trim((' ' + this.getClass(el) + ' ').replace(' ' + name + ' ', ' ')));
        }
    }
    /** 设置dom对象的class */
    public static setClass(el: any, name: string) {
        if (el.className.baseVal === undefined) {
            el.className = name;
        } else {
            el.className.baseVal = name;
        }
    }
    /** 获取dom对象的class */
    public static getClass(el: any) {
        return el.className.baseVal === undefined ? el.className : el.className.baseVal;
    }

    public static setRotate(el: any, rotate: number) {
        let trans = '';
        if (el.style[DomUtils.TRANSFORM]) {
            trans = el.style[DomUtils.TRANSFORM];
        }
        el.style[DomUtils.TRANSFORM] = trans + ' rotate(' + rotate + 'deg)';
    }

    public static setTransform(el: HTMLElement, offset: Pixel, scale?: number, rotate?: number) {
        let pos = offset || new Pixel(0, 0);
        el.style[DomUtils.TRANSFORM] =
            (Browser.ie3d ?
                'translate(' + pos.getX() + 'px,' + pos.getY() + 'px)' :
                'translate3d(' + pos.getX() + 'px,' + pos.getY() + 'px,0)') +
            (scale ? ' scale(' + scale + ')' : '') + (rotate && rotate != 0 ? ' rotate(' + rotate + 'deg)' : '');
    }

    public static setPosition(el: any, point: Pixel) {
        el._leaflet_pos = point;
        if (Browser.any3d) {
            DomUtils.setTransform(el, point);
        } else {
            el.style.left = point.getX() + 'px';
            el.style.top = point.getY() + 'px';
        }
    }

    public static getPosition(el: any): Pixel {
        return el._leaflet_pos || new Pixel(0, 0);
    }

    /** 显示dom对象 */
    public static show(el: any) {
        if (el.style.display == 'none') {
            el.style.display = '';
        }
    }
    /** 隐藏dom对象 */
    public static hide(el: any) {
        el.style.display = 'none';
    }
    /** 阻止鼠標冒泡 */
    public static stopEvents(e: MouseEvent): void {
        if (e.preventDefault)
            e.preventDefault();
        if (e.stopPropagation)
            e.stopPropagation();
    }

    public static testProp(props: Array<string>) {
        var style = document.documentElement.style;
        for (var i = 0; i < props.length; i++) {
            if (props[i] in style) {
                return props[i];
            }
        }
        return undefined;
    }

    /** 获取鼠标相对坐标 */
    public static getContainerPixel(e: MouseEvent | MouseEvent | TouchEvent, left: number = 0, top: number = 0): Pixel {
        let x = 0;
        let y = 0;
        if (e['targetTouches'] !== undefined) {
            x = e['targetTouches'][0].clientX - left;
            y = e['targetTouches'][0].clientY - top;
        } else {
            x = (e['pageX'] || e['clientX']) - left;
            y = (e['pageY'] || e['clientY']) - top;
        }
        return new Pixel(x, y);
    }
    public static getOffsetByBody(el: any) {
        let offsetTop = 0;
        let offsetLeft = 0;
        while (el && el.tagName !== 'BODY') {
            offsetTop += el.offsetTop;
            offsetLeft += el.offsetLeft;
            el = el.offsetParent;
        }
        return { left: offsetLeft, top: offsetTop };
    }

    public static getElementsByClassName(className, tag, parent) {
        tag = tag || "*";
        parent = parent || document;
        if (parent.getElementsByClassName) return parent.getElementsByClassName(className);
        for (var current, elements = parent.getElementsByTagName(tag), returnElements = [], i = 0; i < elements.length; i++) {
            current = elements[i];
            DomUtils.hasClassName(current, className) && returnElements.push(current);
        }
        return returnElements;
    }

    public static hasClassName(ele, className) {
        var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
        return testClass.test(ele.className);
    }
    /** 提示 */
    public static warn(text: string) {
        let div = DomUtils.create('div');
        div.innerHTML = text;
        document.body.appendChild(div);
        div.style.cssText = `
            pointer-events: none;
            position: absolute;
            top: -100px;
            background: rgba(39, 50, 56, .8);
            color: rgb(255, 255, 255);
            padding: 10px 40px;
            letter-spacing: 1px;
            font-size: 15px;
            border-radius: 5px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
            z-index:100;
            `;
        div.style.left = `calc(50vw - ${div.offsetWidth / 2}px)`;
        div.style.transition = "top 250ms ease-out";
        div.style.top = "20px";
        window.setTimeout(() => {
            document.body.removeChild(div);
        }, 3000);
    }
}